package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.controller.VolunteerController;
import com.itheima.domain.VolunteerInfo;
import com.itheima.domain.VolunteerInfoVo;
import com.itheima.mapper.VolunteerInfoMapper;
import com.itheima.service.VolunteerInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Author 12441
 * @Date 2022/9/20 10:12
 * @Version 1.0
 */
@Slf4j
@Service
public class VolunteerInfoServiceImpl extends ServiceImpl<VolunteerInfoMapper, VolunteerInfo> implements VolunteerInfoService {
    @Override
    public VolunteerInfo getVolunteerById(long id) {
        //1.参数校验
        if ((ObjectUtil.isEmpty(id))) {
            throw new RuntimeException();
        }
        //2.查询数据库
        VolunteerInfo volunteerInfo = this.getById(id);

        //3.结果校验
        if (( volunteerInfo == null)) {
            throw new RuntimeException();
        }

        //4.返回结果
        return volunteerInfo;
    }

    @Override
    public void addVolunteer(VolunteerInfo volunteerInfo) {
        //1.结果校验
        if (( volunteerInfo == null)) {
            throw new RuntimeException();
        }
        //2.更新数据库
        this.save(volunteerInfo);
    }

    @Override
    public void deleteVolunteer(long id) {
        //1.参数校验
        if ((ObjectUtil.isEmpty(id))) {
            throw new RuntimeException();
        }
        //2.删除数据库信息
        this.removeById(id);

    }

    @Override
    public void updateVolunteer(VolunteerInfoVo vo) {
        VolunteerInfo volunteerInfo = BeanUtil.copyProperties(vo, VolunteerInfo.class);

        //更新数据库
        this.updateById(volunteerInfo);
    }
}

